﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contracts;
using System.ServiceModel;
using WcfContrib.Client;

namespace Showcase.Client
{
    class Program
    {
        //NOTE: Check OutputCache related notes in Contracts.cs

        static void Main(string[] args)
        {
            ClientChannel<IService1>.LoadRuntime();
            ClientChannel<IService2>.LoadRuntime();

            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Press any key to make a call to the service, 'Enter' to exit'");

                if (Console.ReadKey().Key == ConsoleKey.Enter)
                {
                    break;
                }

                try
                {
                    //See different methods - comment out one of them to see the changes.
                    //TestClientCallWithoutCache();
                    TestWithOutputCacheOnContract();
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Error: " + ex.ToString());
                }
            }

            ClientChannel<IService1>.Instance.Dispose();
            ClientChannel<IService2>.Instance.Dispose();
        }

        static void TestClientCallWithoutCache()
        {
            Foo f = ClientChannel<IService1>.Instance.Channel.GetFoo(
                new[] { new Foo { Id = 1, Name = "Some Foo" }, new Foo { Id = 2, Name = "Some Foo 2" } });

            Console.ForegroundColor = ConsoleColor.White;
            if (f != null)
            {
                Console.WriteLine("Result: Foo Id: " + f.Id + " (call counter)");
            }
            else
            {
                Console.WriteLine("Result: null");
            }
        }

        static void TestWithOutputCacheOnContract()
        {
            Foo f = ClientChannel<IService2>.Instance.Channel.GetFoo(
                new[] { new Foo { Id = 1, Name = "Some Foo" }, new Foo { Id = 2, Name = "Some Foo 2" } });

            Console.ForegroundColor = ConsoleColor.White;
            if (f != null)
            {
                Console.WriteLine("Result: Foo Id: " + f.Id + " (call counter)");
            }
            else
            {
                Console.WriteLine("Result: null");
            }
        }
    }
}
